package com.skycode5.servlet;

import com.skycode5.dao.NewsDao;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet("/newsDel.jsp")
public class NewsDelServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        int NewsId = Integer.parseInt(req.getParameter("NewsId"));
        int result = (new NewsDao()).delete(NewsId);
        if (result > 0) {
            resp.sendRedirect("news.jsp");//参数是相对网址
        } else {
            req.setAttribute("msg", "删除失败");//错误页面显示的内容
            req.setAttribute("url", "news.jsp");//显示错误信息后要跳转的页面
            req.getRequestDispatcher("news.jsp").forward(req, resp);
        }
    }
}
